﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VSToolWindow.xaml.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Interaction logic for VSToolWindow.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

// ReSharper disable InconsistentNaming

using System.Security;

namespace Mar3ek.HouseLight
{
    using System;
    using System.Runtime.InteropServices;
    using LighthouseNET;

    /// <summary>
    /// Interaction logic for VSToolWindow.xaml
    /// </summary>
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public partial class VSToolWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="VSToolWindow"/> class.
        /// </summary>
        public VSToolWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Loaded event of the UserControl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(Properties.Settings.Default.Account) || ((string.IsNullOrEmpty(Properties.Settings.Default.Username) || string.IsNullOrEmpty(Properties.Settings.Default.Password)) && string.IsNullOrEmpty(Properties.Settings.Default.Token)))
            {
                RequestLogin();
            }
            else
            {
                try
                {
                    DataContext = !string.IsNullOrEmpty(Properties.Settings.Default.Token) ? new ToolWindowViewModel(this, new LighthouseAPI(Properties.Settings.Default.Token, Properties.Settings.Default.Account)) : new ToolWindowViewModel(this, new LighthouseAPI(Properties.Settings.Default.Username, Properties.Settings.Default.Password, Properties.Settings.Default.Account));
                    btnLogin.ToolTip = "Switch user";
                }
                catch (SecurityException)
                {
                    RequestLogin();
                }
            }
        }

        /// <summary>
        /// Requests the login screen.
        /// </summary>
        private void RequestLogin()
        {
            var w = new wLogin() { Owner = this };
            if (w.ShowDialog() != true)
            {
                return;
            }

            if (DataContext != null && DataContext is IDisposable)
            {
                ((IDisposable)DataContext).Dispose();
            }

            var token = Properties.Settings.Default.Token;
            var account = Properties.Settings.Default.Account;
            var username = Properties.Settings.Default.Username;
            var password = Properties.Settings.Default.Password;

            try
            {
                DataContext = !string.IsNullOrEmpty(token) ? new ToolWindowViewModel(this, new LighthouseAPI(token, account)) : new ToolWindowViewModel(this, new LighthouseAPI(username, password, account));
            }
            catch (SecurityException)
            {
                RequestLogin();
            }

            btnLogin.ToolTip = "Switch user";
        }

        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnLogin_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Properties.Settings.Default.Username = string.Empty;
            Properties.Settings.Default.Password = string.Empty; 
            Properties.Settings.Default.Token = string.Empty;
            Properties.Settings.Default.Save();

            RequestLogin();
        }

        /// <summary>
        /// Handles the Click event of the Button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ppHistory.IsOpen = false;
        }
    }
}

// ReSharper restore InconsistentNaming